package com.biz.primus.ms.commodity.dao.redis;

import com.biz.primus.base.constant.Constant;
import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.base.redis.util.RedisUtil;
import com.biz.primus.ms.commodity.dao.ro.BrandRo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * 品牌 RedisDao
 *
 * @author david-liu
 * @date 2017年01月04日
 * @reviewer
 * @see
 */
@Repository
public class BrandRedisDao extends CrudRedisDao<BrandRo, Long> implements Serializable {

    private static final long serialVersionUID = -1784004848159840673L;

    public List<BrandRo> getAppBrand(Long categoryId) {
        if (categoryId == null) {
            categoryId = Constant.DEFAULT_CATEGORY_ID;
        }
        Set<byte[]> bytes = zRange(getAppBrandSortSetKey(categoryId), 0, -1);
        return findByIds(bytes);
    }

    public List<String> getBrandIdsByCategory(Long categoryId) {
        if (categoryId == null) {
            return Lists.newArrayList();
        }
        Set<byte[]> bytes = zRange(getAppBrandSortSetKey(categoryId), 0, -1);
        if (CollectionUtils.isEmpty(bytes)) {
            return Lists.newArrayList();
        }
        return RedisUtil.bytesSetToStringList(bytes);
    }


    /**
     * * 获取快喝品牌商城 品牌排序
     *
     * @param categoryId
     * @return
     */
    public String getAppBrandSortSetKey(Long categoryId) {
        return getKeyByParams(categoryId, "App");
    }

    /**
     * 设置快喝品牌商城分类下品牌排序
     *
     * @param brandId
     * @param categoryIds
     * @param idx
     */
    public void setAppBrandSort(Long brandId, List<Long> categoryIds, Double idx) {
        for (Long categoryId : categoryIds) {
            zadd(getAppBrandSortSetKey(categoryId), idx, RedisUtil.toByteArray(brandId));
        }
    }

    /**
     * 删除快喝品牌商城品牌排序
     *
     * @param brandId
     * @param categoryIds
     */
    public void removeAppSort(Long brandId, List<Long> categoryIds) {
        if (brandId == null || CollectionUtils.isEmpty(categoryIds)) {
            return;
        }
        for (Long categoryId : categoryIds) {
            zrem(getAppBrandSortSetKey(categoryId), RedisUtil.toByteArray(brandId));
        }
    }


}
